package org.tity.commons.environment;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * @author fangjieli 
 * */
public class CommandShell {

	/**
	 * 执行本机命令行
	 * @param command 命令
	 * @return 命令输出
	 * */
	public static String exec(String command) {

		BufferedReader br = null;
		try {
			Process p = Runtime.getRuntime().exec(command);
			br = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName("GBK")));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
			return sb.toString();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
		
		return null;

	}
	
	
	public static void main(String[] args) {
		exec("netstat -nao");
//		exec("ping 127.0.0.1");
	}

}
